/**
 *
 */
package de.guidoludwig.jtm.client.shows;

import java.util.List;

import de.guidoludwig.jtm.domain.Archive;
import de.guidoludwig.jtm.domain.Country;
import de.guidoludwig.jtm.domain.Media;
import de.guidoludwig.jtm.domain.Show;

/**
 * @author guido Read Only Format to convert Show into a beautiful HTML String
 */
public class ShowHTMLFormat extends HTMLFormat<Show> {

	public static final ShowHTMLFormat INSTANCE = new ShowHTMLFormat();
	private static final String NBSP = "&nbsp;";

	@Override
	public String buildContent(Show a) {
		StringBuilder toAppendTo = new StringBuilder();
		toAppendTo.append("<table><tr><td width=\"50%\">" + buildLeftContent(a) + "</td><td width=\"50%\">" + buildRightContent(a) + "</td></tr></table>");
		return toAppendTo.toString();
	}

	public String buildLeftContent(Show a) {
		StringBuilder toAppendTo = new StringBuilder();
		toAppendTo.append("<table>")
			.append(buildTableRow("Date", formatShowDate(a)))
			.append(buildTableRow("Venue", a.getLocation().getVenue()))
			.append(buildTableRow("City", a.getLocation().getCity()))
			.append(buildTableRow("Country", formatCountry(a.getLocation().getCountry())))
			.append(buildTableRow("Archive", formatArchive(a.getArchive())))
     		.append(buildTableRow("Media", formatMedia(a.getMedia())))
			.append("</table>");
		return toAppendTo.toString();
	}
	public String buildRightContent(Show a) {
		StringBuilder toAppendTo = new StringBuilder();
		toAppendTo.append(toHTMLLines(a.getShowRemarks() + "\n" + a.getRecordingRemarks()));
		return toAppendTo.toString();
	}

	private String formatMedia(List<Media> media) {
		if (media == null) {
			return NBSP;
		}
		StringBuilder b = new StringBuilder();
		for (Media m : media) {
			if (b.length() > 0) {
				b.append("<br/>");
			}
			b.append(formatMedia(m));
		}
		return b.toString();
	}

	private Object formatMedia(Media m) {
		if (m == null) {
			return NBSP;
		}
		return m.getMediaType() + " " + m.getQuantity() + " " + m.getKbps()
				+ " " + m.getVideoSystem();
	}

	private String formatArchive(Archive archive) {
		if (archive == null) {
			return NBSP;
		}
		return archive.getCode() + " - " + archive.getName();
	}

	private String formatCountry(Country country) {
		if (country == null) {
			return NBSP;
		}
		return country.name() + " - " + country.getCountryName();
	}



	private String formatShowDate(Show a) {
		return a.getShowDate() == null ? NBSP : a.getShowDate().toString();
	}



	@Override
	protected String getTitle(Show t) {
		return t.getArtist().getName() + " " + t.getName();
	}

}
